import moment from 'moment'
import Cookies from 'js-cookie'
import echarts from 'echarts'

/*
t: 时间戳、 ISO 8601格式的日期字符串
f: 格式 YYYY-MM-DD HH:mm:ss
*/
export function formatDate(t, f = 'YYYY-MM-DD HH:mm:ss') {
  const date = moment(t)

  return date.isValid() ? date.format(f) : '-'
}

export const getExplorer = () => {
  const ua = window.navigator.userAgent
  const isExplorer = (exp) => {
    return ua.indexOf(exp) > -1
  }
  if (!!window.ActiveXObject || 'ActiveXObject' in window) return 'IE'
  else if (isExplorer('Firefox')) return 'Firefox'
  else if (isExplorer('Chrome')) return 'Chrome'
  else if (isExplorer('Opera')) return 'Opera'
  else if (isExplorer('Safari')) return 'Safari'
}

export const isIE = () => {
  return getExplorer() === 'IE'
}

export const isArray = (obj) => {
  return Object.prototype.toString.apply(obj) === '[object Array]'
}

export const isObject = (obj) => {
  return Object.prototype.toString.apply(obj) === '[object Object]'
}

export const isNumber = (obj) => {
  return Object.prototype.toString.apply(obj) === '[object Number]'
}

/**
 * @description 绑定事件 on(element, event, handler)
 */
export const on = (function() {
  if (document.addEventListener) {
    return function(element, event, handler) {
      if (element && event && handler) {
        element.addEventListener(event, handler, false)
      }
    }
  } else {
    return function(element, event, handler) {
      if (element && event && handler) {
        element.attachEvent('on' + event, handler)
      }
    }
  }
})()

/**
 * @description 解绑事件 off(element, event, handler)
 */
export const off = (function() {
  if (document.removeEventListener) {
    return function(element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false)
      }
    }
  } else {
    return function(element, event, handler) {
      if (element && event) {
        element.detachEvent('on' + event, handler)
      }
    }
  }
})()

export const clone = (origin) => {
  return JSON.parse(JSON.stringify(origin))
}

export function getTheme() {
  return Cookies.get('activeTheme') ? Cookies.get('activeTheme') : 'theme-default'
}

export function setTheme(theme) {
  Cookies.set('activeTheme', theme)
  toggleClass(document.body, theme)
}

export function getActiveMenu() {
  // return Cookies.get('activeTopMenu') ? Cookies.get('activeTopMenu') : ''
  return 'dcom'
}

export function setActiveMenu(topmenu) {
  Cookies.set('activeTopMenu', topmenu)
}

export function toggleClass(element, className) {
  if (!element || !className) {
    return
  }
  const echartsThemeModule = require('../styles/' + className + '/echarts-theme.js')
  echarts.registerTheme('echartsTheme', echartsThemeModule.theme)
  element.className = className
}

/**
 * @description 获取月的开始时间
 * @param {Number} month 月份
 */
export function getStartMonth(year = new Date().getFullYear(), month = new Date().getMonth()) {
  const result = new Date(year, month, 1)
  result.setHours(0)
  result.setMinutes(0)
  result.setSeconds(0)
  return result
}

/**
 *  @description 获取月的结束时间
 * @param {Number} month
 */
export function getEndMonth(year = new Date().getFullYear(), month = new Date().getMonth() + 1) {
  const result = new Date(year, month, 0)
  result.setHours(23)
  result.setMinutes(59)
  result.setSeconds(59)
  return result
}

/**
 * @description 几月前后
 */
export function addMonths(v, n) {
  if (v) {
    var t = new Date(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds(), v.getMilliseconds())
    t.setMonth(v.getMonth() + n)
    if (t.getDate() !== v.getDate()) { t.setDate(0) }
    return t
  }
}

/**
 *
 * @description 几日前后
 */
export function addDays(v, n) {
  if (v) {
    v = new Date(v.valueOf())
    v.setDate(v.getDate() + n)
    return v
  }
}

/**
 *
 * @description 计算两个时间相差的月份
 */
export function diffMonth(begin, end) {
  begin = moment(begin)
  const beginMonth = begin.year() * 12 + (begin.month() + 1)

  end = moment(end)
  const endMonth = end.year() * 12 + (end.month() + 1)

  return endMonth - beginMonth + 1
}

export function monthBegin(time) {
  time = moment(time)
  return time.month(time.month()).startOf('month').valueOf()
}

export function monthEnd(time) {
  time = moment(time)
  return time.month(time.month()).endOf('month').valueOf()
}

export function weekBegin(time) {
  time = moment(time)
  return time.week(time.week()).startOf('week').valueOf()
}

export function weekEnd(time) {
  time = moment(time)
  return time.week(time.week()).endOf('week').valueOf()
}

export function getWeekOfYear(time) {
  const date = moment(time).toDate()
  const firstDateOfYear = new Date(date.getFullYear(), 0, 1)

  let today = date.getDay()
  if (today === 0) today = 7

  let firstDayOfYear = firstDateOfYear.getDay()
  if (firstDayOfYear === 0) firstDayOfYear = 7

  const d = Math.round((date.getTime() - firstDateOfYear.getTime() + (firstDayOfYear - today) * (24 * 60 * 60 * 1000)) / 86400000)
  return Math.ceil(d / 7) + 1
}

export function getFirstDayOfWeekOfYear(time) {
  const d = moment(time)
  const date = d.subtract((d.weekday() === 0 ? 7 : d.weekday()) - 1, 'd')
  const d2 = moment(date.toDate())
  return new Date(d2.year(), d2.month(), d2.date())
}
